package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pojo.pro.WorkflowProduct;
import com.qidianit.mes.pro.mapper.WorkflowProductMapper;
import com.qidianit.mes.pro.service.WorkflowProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Author: YanJiRan
 * @Date: 2025/9/19 - 下午5:12
 */
@Service
public class WorkflowProductServiceImpl implements WorkflowProductService {
    @Autowired
    private WorkflowProductMapper workflowProductMapper;

    @Override
    public Result<PageInfo<WorkflowProduct>> selectList(WorkflowProduct workflowProduct, int page, int size) {
        //开启分页
        PageHelper.startPage(page,size);
        List<WorkflowProduct> workflowProducts = workflowProductMapper.selectList(workflowProduct);
        return Result.success(new PageInfo<>(workflowProducts));
    }

    @Override
    public Result<WorkflowProduct> saveWorkflowProduct(WorkflowProduct workflowProduct) {
        //同一工艺下是否已存在相同的产品/物料
        WorkflowProduct checkCondition = new WorkflowProduct();
        checkCondition.setWorkflowId(workflowProduct.getWorkflowId());
        //同一工艺下的所有产品/物料
        List<WorkflowProduct> products = workflowProductMapper.selectList(checkCondition);
        for (WorkflowProduct product : products) {
            //就是本身id
            if (!product.getId().equals(workflowProduct.getId())) {
                //产品/物料编码是否重复
                if (product.getProductCode().equals(workflowProduct.getProductCode())) {
                    return Result.error("工序该产品/物料已经添加");
                }
            }
        }
        return workflowProductMapper.saveWorkflowProduct(workflowProduct) == 1 ? Result.success(workflowProduct) : Result.error("操作失败");
    }

    @Override
    public Result<WorkflowProduct> selectById(Long id) {
        return Result.success(workflowProductMapper.selectById(id));
    }

    @Override
    public Result updateById(WorkflowProduct workflowProduct) {
        //同一工艺下是否已存在相同的产品/物料
        WorkflowProduct checkCondition = new WorkflowProduct();
        checkCondition.setWorkflowId(workflowProduct.getWorkflowId());
        //同一工艺下的所有产品/物料
        List<WorkflowProduct> products = workflowProductMapper.selectList(checkCondition);
        for (WorkflowProduct product : products) {
            //就是本身id
            if (!product.getId().equals(workflowProduct.getId())) {
                //产品/物料编码是否重复
                if (product.getProductCode().equals(workflowProduct.getProductCode())) {
                    return Result.error("该产品/物料已经添加");
                }
            }
        }
        return workflowProductMapper.updateById(workflowProduct) == 1 ? Result.success() : Result.error("操作失败");
    }

    @Override
    public Result deleteById(Long id) {
        return workflowProductMapper.deleteById(id) == 1 ? Result.success() : Result.error("操作失败");
    }

    @Override
    public Result batchDelete(Long[] ids) {
        return workflowProductMapper.batchDelete(ids) > 0 ? Result.success() : Result.error("操作失败");
    }
}
